<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends MY_Controller {
    
    var $controller_path 	    = '/role/index/';
    var $model_path 		    = 'role/role_model';
    var $lang_path 		    = 'role/role';
	
    function __construct()
    {
		parent::__construct();
		log_message('debug', "role/index Index controller Class Initialized.");
    }


    public function find_all()
    {
        //
        $role_arr = $this->role_model->alls();
        $this->api(200,$role_arr,"");
    }

    public function save_role_menus($id = 0)
    {
        if($id <= 0) $this->api("304",NULL,"没有选择角色");
        $post_data = $this->get_input("","array");
        if(!$post_data) $this->api("304",NULL,"数据为空");

        $this->load->model("role/role_menu_model");
        //删除数据
        if(!$this->role_menu_model->dels($id)) $this->api("304",NULL,"删除失败");

        //添加数据
        if(!$this->role_menu_model->adds($post_data)) $this->api("304",NULL,"添加失败");

        $this->api("200",NULL,"");
    }

    public function find_role_menus()
    {
        $id = $this->input->get("roleId");
        $role_info = $this->role_model->info($id);
        if(!$role_info) $this->api("304",NULL,"角色信息为空");
        $this->load->model("role/role_menu_model");
        $this->load->model("menu/menu_model");
        $menu_arr = NULL;
        if($role_info->isroot == 1)
        {
            $menu_arr = $this->menu_model->get_role_menu();
        }
        else
        {
            $menu_arr = $this->role_menu_model->get_menu_all($role_info->id);
        }

        $this->api("200",$menu_arr,"");
    }
	
    public function index()
    {
        $map_arr = NULL;
        $seach_arr = $this->get_input("columnFilters"); //获得数据
        //print_r($seach_arr);
        $map_arr["name"] = $seach_arr->name->value;

        $total_nums = $this->role_model->lists(NULL,$map_arr);
        $ret_arr = $this->get_page($total_nums,$this->get_input("pageNum"),$this->get_input("pageSize"));
        $ret_arr["data"]["content"] = $this->role_model->lists($ret_arr["limit"],$map_arr);
        if($ret_arr["data"]["content"])
        {
        	foreach($ret_arr["data"]["content"] as &$var)
        	{
        		//
                $var -> createTime = $this->time($var->createTime);
        	}
        }
		$this->api("200",$ret_arr["data"],"");
    }

    public function delete()
    {
        $ret_data['type'] = 304;
        $ret_data['message'] = '';
        $ret_data['data'] = NULL;

        $ids = $this->get_input();
        $ids_arr = NULL;
        $type = false;
        if($ids)
        {
            foreach($ids as $val)
            {
                $ids_arr[] = $val->id;
            }
            $type = $this->role_model->dels($ids_arr);
        }


        if($type) {
            $ret_data['type'] = 200;
            $ret_data['message'] = $this->lang->line('role_success');
        }
        else {
            $ret_data['message'] = $this->lang->line('role_error');
        }
        $this->write_logs($ret_data['type'],$ret_data['data'],"角色删除：".$ret_data['message']);
        $this->api($ret_data['type'],$ret_data['data'],$ret_data['message']);
    }

    public function save()
    {
        $ret_data['type'] = 304;
        $ret_data['message'] = '';
        $ret_data['data'] = NULL;

        $post_data = $this->get_input("","array");
        $this->form_validation->set_data($post_data);
        $this->form_validation->dir_set_rules('role/role');
        if($this->form_validation->run() == true) {

            //if($post_data["id"]) $data_arr['id'] = $post_data["id"];

            $data_arr['name'] = $post_data["name"];
            $data_arr['remark'] = $post_data["remark"];

            if($post_data["id"])
            {
                $type = $this->role_model->edits($post_data["id"],$data_arr);
            }
            else
            {
                $type = $this->role_model->adds($data_arr);
            }

            if($type) {
                $ret_data['type'] = 200;
                $ret_data['data'] = $type;
                $ret_data['message'] = $this->lang->line('role_success');
            }
            else {
                $ret_data['message'] = $this->lang->line('role_error');
            }
        }
        else {
            $ret_data['message'] = $this->form_validation->error_string();
        }
        $this->write_logs($ret_data['type'],$ret_data['data'],"角色更新：".$ret_data['message']);
        $this->api($ret_data['type'],$ret_data['data'],$ret_data['message']);
    }
}
